package view;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.StuinfoDao;
import entity.StuInfo;

public class UpdateShow extends JPanel implements ActionListener{
      /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private StuInfo stuInfo;
	private StuinfoDao stuinfoDao;
	
	
	JTextField[] showtf=new JTextField[3];
	JTextField intf;
	
	public UpdateShow() {
		JPanel pan =new JPanel();
		pan.setLayout(new GridBagLayout());
		
		 String[] str={"学号","姓名","性别"};
	        GridBagConstraints gbc = new GridBagConstraints();
	        
	        JLabel inla=new JLabel("请输入您要修改的学生学号");
	        gbc.gridx=1;
        	gbc.gridy=0;
        	gbc.insets=new Insets(20,20,20,20);
        	pan.add(inla,gbc);
	        
            intf=new JTextField(5);
        	gbc.gridx=2;
        	gbc.gridy=0;
        	gbc.insets=new Insets(20,20,20,20);
        	pan.add(intf,gbc);
	        
        	
        	JButton inbt=new JButton("确定");
        	gbc.gridx=3;
        	gbc.gridy=0;
        	gbc.insets=new Insets(20,20,20,20);
        	inbt.addActionListener(this);
        	pan.add(inbt,gbc);
        	
        	
	        for(int i=0;i<str.length;i++){
	        	JLabel la=new JLabel(str[i]);
	        	gbc.gridx=0;
	        	gbc.gridy=1+i;
	        	gbc.insets=new Insets(20,20,20,20);
	        	pan.add(la,gbc);
	        
	        
	        	showtf[i]=new JTextField(20);
	        	gbc.gridx=1;
	     
	        	pan.add(showtf[i],gbc);
	 
	        }
	        showtf[0].setEnabled(false);
			showtf[0].setDisabledTextColor(Color.BLACK);
	        JButton bt1=new JButton("确定修改");
	        gbc.gridx=2;
        	gbc.gridy=6;
        	gbc.insets=new Insets(20,20,20,20);
        	bt1.addActionListener(this);
        	pan.add(bt1,gbc);
        	
        	
        	JButton bt2=new JButton("取消");
	        gbc.gridx=3;
        	gbc.gridy=6;
        	gbc.insets=new Insets(20,20,20,20);
        	pan.add(bt2,gbc);
	        
	        add(pan);
		
		
		
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
     String cmd=e.getActionCommand();
     switch (cmd) {
	case "确定":
		stuInfo=new StuInfo();
		stuinfoDao=new StuinfoDao();
		stuInfo.setId(Integer.valueOf(intf.getText()));
		stuInfo=stuinfoDao.getOne(stuInfo);
		showtf[0].setText(String.valueOf(stuInfo.getId()));
		showtf[1].setText(stuInfo.getName());
		showtf[2].setText(stuInfo.getSex());
		System.out.println("点击确定");
		break;
  
	case "确定修改":
		stuInfo=new StuInfo();
		stuinfoDao=new StuinfoDao();
		stuInfo.setName(showtf[1].getText());
		stuInfo.setSex(showtf[2].getText());
		stuInfo.setId(Integer.valueOf(showtf[0].getText()));
		stuinfoDao.update(stuInfo);
		JOptionPane.showMessageDialog(this, "修改成功");
		System.out.println("修改成功");
		
		break;
	}
		
	}
	
	
	
	
}
